<template>
  <div style="display: flex;align-items: center;justify-content: center">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="font-size: 18px">修改密码</span>
      </div>
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
               label-width="80px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="dataForm.phone" type="number" clearable placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <div style="display: flex">
            <el-input v-model="dataForm.code" type="number" clearable placeholder="验证码"
                      style="margin-right: 30px"></el-input>
            <el-button type="primary" @click="getCode">{{ codeDesc }}</el-button>
          </div>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="dataForm.password" type="password" show-password clearable placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item>
          <div style="display: flex;justify-content: center;align-items: center">
            <el-button type="primary" style="width:250px" @click="dataFormSubmit">确认修改</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "update-password",
  data() {
    return {
      dataForm: {
        password: '',
        phone: '',
        code: ''
      },
      dataRule: {
        phone: [
          {required: true, message: '手机号不能为空', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '验证码不能为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'blur'}
        ],
      },
      codeDesc: "获取验证码",
      secNumY:60,
      secNum:60,
      timer:null
    }
  },
  methods: {
    getCode() {
      this.countdown();
      this.$http({
        url: this.$http.adornUrl(`/sys/user/send/sms`),
        method: 'post',
        data: this.$http.adornData({
          'phone': this.dataForm.phone,
        })
      }).then(({data}) => {
        if (data.success) {
          this.$message({
            message: '发送成功',
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.countdown();
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    countdown(){
      this.timer = setInterval(() => {
        if (--this.secNum) {
          // 用当前倒计时的秒数替换提示字符串中的"x"字母
          this.codeDesc= this.secNum+"s后重试"
        } else {
          clearInterval(this.timer);
          this.timer = null;
          this.secNum = this.secNumY;
          this.codeDesc = "重新获取"
        }
      }, 1000);
    },

    dataFormSubmit(){
      this.$http({
        url: this.$http.adornUrl(`/sys/user/update/password`),
        method: 'put',
        data: this.$http.adornData(this.dataForm)
      }).then(({data}) => {
        if (data.success) {
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.$router.back()
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    }

  }
}
</script>

<style scoped>
.box-card {
  width: 40%;
  margin-top: 120px;
}
</style>
